package com.mobile.collect.present;

import com.mobile.collect.activitys.ExportListActivity;
import com.mobile.collect.beans.DeviceInfo;
import com.mobile.collect.beans.DevicePicInfo;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.v3.TipDialog;
import com.mobile.collect.utils.ExportUtil2;

import java.util.List;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.mvp.XPresent;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 类名: ExprotListPresent
 * 作者: 李学伟
 * 日期: 2020年09月30日 10:26
 * 描述:
 */
public class ExprotListPresent extends XPresent<ExportListActivity> {

    public void checkExport(List<DeviceInfo> deviceInfoList) {
        if (Kits.Empty.check(deviceInfoList)) {
            getV().showExportDialog(false, "请选择导出设备！");
            return;
        }
        boolean hadExport = false;
        for (DeviceInfo deviceInfo : deviceInfoList) {
            List<DevicePicInfo> picInfos = deviceInfo.getDevicePicList();
            if (Kits.Empty.check(picInfos)) {
                getV().showExportDialog(false, deviceInfo.getDeviceName() + "没有填入检测单元，请添加后导出！");
                return;
            }
            for (DevicePicInfo picInfo : picInfos) {
                List<DevicePointInfo> pointList = picInfo.getPointList();
                if (Kits.Empty.check(pointList)) {
                    getV().showExportDialog(false, deviceInfo.getDeviceName() + picInfo.getUnitName() + "没有添入检测标签，请添加后导出！");
                    return;
                }
                for (DevicePointInfo devicePointInfo : pointList) {
                    int screenX = devicePointInfo.getScreenX();
                    int screenY = devicePointInfo.getScreenY();
                    if (screenX == 0 && screenY == 0) {
                        String pic = picInfo.getDeviceName() + "-" + picInfo.getLabel();
                        getV().showErrorDialog( picInfo.get_ID(),picInfo.getPicPath(),"存在错误点，重新打开图片后可继续导出，请查看【" + pic+ "】！");
                        return;
                    }
                }



                int picWidth = picInfo.getPicBitMapWidth();
                int picHeight = picInfo.getPicBitmapHeight();
                if (picWidth == 0 && picHeight == 0) {
                    getV().showExportDialog(false, "请确定【" + deviceInfo.getDeviceName() + picInfo.getUnitName() + "】已添加标签！");
                    return;
                }


            }
            if (!hadExport) {
                hadExport = deviceInfo.isHadExport();
            }


        }
        String message = "是否导出所选设备检测信息！立即导出？";
        if (hadExport) {
            message = "存在\"已导出设备信息\"，是否继续导出已选设备信息。立即导出？";
        }
        getV().showExportDialog(true, message);

    }

    public void export(final List<DeviceInfo> deviceInfoList) {
        getV().showLoadDialog();
        Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                ExportUtil2 exportUtil = new ExportUtil2(getV());
                try {
                    exportUtil.start(deviceInfoList);
                    subscriber.onNext(1);
                } catch (Exception e) {
                    subscriber.onError(e);
                    e.printStackTrace();
                }

            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                getV().disLoadDialog();
                TipDialog.show(getV(), e.getMessage(),TipDialog.TYPE.ERROR);
                e.printStackTrace();
            }

            @Override
            public void onNext(Object o) {
                getV().disLoadDialog();

                for (DeviceInfo deviceInfo : deviceInfoList) {
                    deviceInfo.setHadExport(true);
                }
                EntityManager.getInstance().saveDeviceInfo(deviceInfoList);
                TipDialog.show(getV(), "导出完成", TipDialog.TYPE.SUCCESS);
                getV().notifyDataSetChanged();
            }
        });
    }

}
